package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.SysUsrMapper;
import com.example.entity.SysUsr;
import com.example.service.SysUsrService;
import com.example.mapper.SysRoleMapper;
import com.example.entity.vo.SysUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class  SysUsrServiceImpl extends ServiceImpl<SysUsrMapper, SysUsr> implements SysUsrService {
//    @Autowired(required = false)
//    SysUsrMapper sysUsrMapper;
//    @Override
//    public SysUsr login(String usrName, String password) {
//        return sysUsrMapper.login(usrName,password);
//    }
//
//    @Override
//    public List<SysUsr> getAllList(User userPager, String usrName, String roleId) {
//        return sysUsrMapper.getAllList(userPager, usrName, roleId);
//    }

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysUsrMapper sysUserMapper;

    @Override
    public SysUserVo getUserByUsrNameAndPwd(String usrName, String usrPassword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("usr_name", usrName);
        queryWrapper.eq("usr_password", usrPassword);
        SysUsr temp = getOne(queryWrapper);
        if (temp != null) {
            SysUserVo result = new SysUserVo();
            BeanUtils.copyProperties(temp, result);
            result.setRole(sysRoleMapper.selectById(temp.getUsrRoleId()));
            return result;
        }
        return null;
    }

    @Override
    public SysUserVo getUserByUsrName(String usrName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("usr_name", usrName);
        SysUsr temp = getOne(queryWrapper);
        if (temp != null) {
            SysUserVo result = new SysUserVo();
            BeanUtils.copyProperties(temp, result);
            result.setRole(sysRoleMapper.selectById(temp.getUsrRoleId()));
            return result;
        }
        return null;
    }

    @Override
    public Page pageRecords(Page page, QueryWrapper queryWrapper) {

        return sysUserMapper.pageList(page,queryWrapper);
    }
}
